﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Controls.Primitives;

namespace Nok_Air
{
	public partial class ViewBookingPage : PhoneApplicationPage
	{
		private Popup popup;
		public ViewBookingPage()
		{
			popup = new Popup();
			popup.Child = new ProgressBarUserControl();
			InitializeComponent();
			DataContext = App.ViewModel;
            /*
            this.bookingNumberTextBox.Text = "10689329";
            this.lastNameTextBox.Text = "CHAROENSRI";
            this.agreeCheckB.IsChecked = true;   
             * */
            /*
            Nok_Air.WebService.NokAirServicesHelper nok = new WebService.NokAirServicesHelper();
            nok.GetSubmitBookingStatus("5030477", "asd");*/
		}

		void nokSHelp_RequestCompleted(object sender, WebService.RequestCompletedEventArgs ev)
		{
			if (ev.exception != null)
			{
				Dispatcher.BeginInvoke(() =>
				{
					MessageBox.Show("Connection Error");
					NavigationService.GoBack();
					NavigationService.Navigated += (ss, ee) => { popup.IsOpen = false; };
				});
				return;
			}
			Dispatcher.BeginInvoke(() => popup.IsOpen = false);
		}

		private void conditionButton_Click(object sender, RoutedEventArgs e)
		{
			NavigationService.Navigate(new Uri("/CheckIn/TermAndConditionPage.xaml", UriKind.Relative));
		}

		private void continueButton_Click(object sender, RoutedEventArgs e)
		{
            if (String.IsNullOrEmpty(lastNameTextBox.Text) || String.IsNullOrEmpty(bookingNumberTextBox.Text))
            {
                MessageBox.Show(App.ViewModel.FillFormErrorMsg);
                return;
            }

            if(!agreeCheckB.IsChecked.Value)
            {
				MessageBox.Show(App.ViewModel.AgreeErrorMsg);
                return;
            }

			if (bookingNumberTextBox.Text == "" || lastNameTextBox.Text == "") return;
			popup.IsOpen = true;
			NavigationService.Navigate(new Uri("/ViewBooking/BookingResult.xaml", UriKind.Relative));
			Nok_Air.WebService.NokAirServicesHelper nokSHelp = new Nok_Air.WebService.NokAirServicesHelper();
			nokSHelp.GetBookingByBookingNumberCompleted += nokSHelp_RequestCompleted;
			nokSHelp.GetBookingByBookingNumber(bookingNumberTextBox.Text, lastNameTextBox.Text);
		}
		private void agreeTextblock_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			if (agreeCheckB.IsChecked.Value)
			{
				agreeCheckB.IsChecked = false;
			}
			else
			{
				agreeCheckB.IsChecked = true;
			}
		}

        private void lastNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox box = sender as TextBox;
            Validator.validateName(box);
        }
        private void bookNum_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox box = sender as TextBox;
            Validator.validateNumber(box);
        }
	}
}